package com.yy.controller;

import com.yy.pojo.Dept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class DeptController {
    @RequestMapping("/selectDept")
    public String t1() {
        return "index.jsp";
    }

//    @RequestMapping("/listDept")
//    @ResponseBody
//    public List<String> getListString(String str){
//        ArrayList<String> list = new ArrayList<String>();
//        list.add(str);
//        return list;
//    }

    // 响应一个对象
    @RequestMapping("/getDept")
    @ResponseBody
    public Dept getDept() {
        return new Dept(1, "无语部");
    }

    // 响应一个对象集合
    @RequestMapping("/list")
    @ResponseBody
    public List<Dept> getListDept() {
        Dept dept = new Dept(1, "人五部");
        Dept dept1 = new Dept(2, "人六部");
        Dept dept2 = new Dept(3, "人山部");
        Dept dept3 = new Dept(4, "人海部");
        Dept dept4 = new Dept(5, "五四部");
        ArrayList<Dept> list = new ArrayList<Dept>();
        list.add(dept);
        list.add(dept1);
        list.add(dept2);
        list.add(dept3);
        list.add(dept4);
        return list;
    }

    // 响应一个有参的字符串
    @RequestMapping(value = "/toString", produces = "application/json;charset:utf-8") //解决Request响应乱码请求
    @ResponseBody
    // 使用别名的情况
    public String getString(Integer id, @RequestParam(value = "username", required = false, defaultValue = "李白") String name) {
        return id + name;
    }

    @RequestMapping(value = "/toString1", produces = "application/json;charset:utf-8") //解决Request响应乱码请求
    @ResponseBody
    // 不使用别名的情况
    public String getString1(Integer id, String name) {
        return id + name;
    }

    // 响应一个请求获得对象
    @RequestMapping("/getRequestDept")
    @ResponseBody
    public Dept getRequestDept(Dept dept) {
        System.out.println("dept = " + dept);
        return dept;
    }

    // 响应一个字符串类型的数组

    @RequestMapping("/getListString")
    @ResponseBody
    public List<String> getListString(String[] drink) {
        return Arrays.asList(drink);
    }

    // 响应一个对象类型的数组
    @RequestMapping("/getListDept")
    @ResponseBody
    public List<Dept> getListDept(@RequestBody List<Dept> dept) {
        return dept;
    }
}
